const sass = require('node-sass')
const loadGruntTasks = require('load-grunt-tasks')

module.exports = grunt => {
  grunt.initConfig({
    clean: {
      dist: 'dist'
    },
    sass: {
      options: {
        implementation: sass,
        sourceMap: true
      },
      main: {
        files: [{
          expand: true,
          cwd: 'src/assets/styles', 
          src: ['**/*.scss'],
          dest: 'dist/assets/styles',
          ext: '.css',
        }],
      }
    },
    babel: {
      options: {
        presets: ['@babel/preset-env'],
        sourceMap: true
      },
      main: {
        files: [{
          expand: true,
          cwd: 'src/assets/scripts',
          src: ['**/*.js'],
          dest: 'dist/assets/scripts',
          ext: '.js',
        }],
      }
    },
    swig_precompile: {
      options: {
        active: '',
        locals:  require('./options'),
        beautify: {
          indent_size: 2
        }
      },
      dev: {
        options: {
          force: false
        },
        expand: true,
        cwd: "src",
        src: "*.html",
        dest: "dist"
      },
      build: {
        expand: true,
        cwd: "src",
        src: "*.html",
        dest: "dist"
      }
    },
    // 依赖文件合并
    concat: {
      options: {
        separator: ';',
        stripBanners: true
      },
      js: {
        src: [
          "./node_modules/jquery/dist/jquery.js",
          "./node_modules/popper.js/dist/umd/popper.js",
          "./node_modules/bootstrap/dist/js/bootstrap.js",
        ], // 需要构建的js路径  
        dest: "dist/assets/scripts/vendor.js" // 构建后js路径 
      },
      css:{
        src: [
          "./node_modules/bootstrap/dist/css/bootstrap.css"
        ],
        dest: "dist/assets/styles/vendor.css"
      }
    },
    // 处理 html 文件中的构建注释
    useref: {
      html: 'dist/*.html',
      temp: 'dist'
    },
    // copy 一般依赖资源
    copy: {
      src: {
        files: [
          {
            expand: true, 
            cwd: 'src/assets/fonts', 
            src: ['*'], 
            dest: 'dist/assets/fonts'
          },
          {
            expand: true, 
            cwd: 'public', 
            src: ['*'], 
            dest: 'dist'
          }
        ]
      },
      image: {
        files: [
          {
            expand: true, 
            cwd: 'src/assets/images', 
            src: ['*'], 
            dest: 'dist/assets/images'
          }
        ]
      }
    },
    //压缩JS
    uglify: {
      prod: {
        options: {
          sourceMap: true
        },
        files: [{
            expand: true,
            cwd: 'dist/assets/scripts',
            src: ['**/*.js'],
            dest: 'dist/assets/scripts'
        }]
      }
    },

    //压缩CSS
    cssmin: {
      prod: {
        options: {
          report: 'min',
          sourceMap: true
        },
        files: [
          {
            expand: true,
            cwd: 'dist/assets/styles',
            src: ['**/*.css'],
            dest: 'dist/assets/styles'
          }
        ]
      }
    },

    //压缩图片
    imagemin: {
      prod: {
        options: {
          optimizationLevel: 7,
          pngquant: true
        },
        files: [
          {
            expand: true, 
            cwd: 'dist/assets/images', 
            src: ['*'], 
            dest: 'dist/assets/images'
          },
          {
            expand: true, 
            cwd: 'dist/assets/fonts', 
            src: ['*'], 
            dest: 'dist/assets/fonts'
          }
        ]
      }
    },
    //压缩HTML
    htmlmin: {
      options: {
        removeComments: true,
        removeCommentsFromCDATA: true,
        collapseWhitespace: true,
        collapseBooleanAttributes: true,
        removeAttributeQuotes: true,
        removeRedundantAttributes: true,
        useShortDoctype: true,
        removeEmptyAttributes: true,
        removeOptionalTags: true
      },
      html: {
        files: [
          {
            expand: true, 
            cwd: 'dist', 
            src: ['*.html'],
            dest: 'dist'}
        ]
      }
    },
    watch: {
      js: {
        files: ['src/assets/scripts/*.js'],
        tasks: ['babel', 'uglify']
      },
      css: {
        files: ['src/assets/styles/*.scss'],
        tasks: ['sass', 'cssmin']
      }
    }
  })
  loadGruntTasks(grunt)

  grunt.registerTask('build', ['clean', 'sass', 'babel', 'swig_precompile', 'concat', 'useref', 'copy', 'uglify', 'cssmin', 'htmlmin', 'watch'])
}
